﻿using Microsoft.EntityFrameworkCore;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Diagnostics;
using System.Threading;
using MySql.Data.MySqlClient;
using  Dapper;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Stopwatch stopwatch=new Stopwatch();
            //stopwatch.Start();
            //Parallel.For(0, 1000, k =>
            //{
            //    for (int i = 0; i < 100; i++)
            //    {
            //        using (var db = new BloggingContext())
            //        {
            //            Blog blog = new Blog()
            //            {
            //                Url = Guid.NewGuid().ToString()
            //            };
            //            db.Add(blog);
            //            db.SaveChanges();
            //        }
            //    }
            //});
            //stopwatch.Stop();
            //Console.WriteLine("EF Core总计耗时："+ stopwatch.ElapsedMilliseconds+"毫秒");


            //stopwatch.Start();
            //Parallel.For(0, 1000, k =>
            //{
            //    for (int i = 0; i < 100; i++)
            //    {
            //        using (IDbConnection dbConnection = new MySqlConnection("server=127.0.0.1;database=db_test;uid=root;pwd=123456"))
            //        {
            //            Blog blog = new Blog()
            //            {
            //                Url = Guid.NewGuid().ToString()
            //            };
            //            dbConnection.ExecuteScalar(@"insert into tb_blog(url) values (@url)", blog);
            //        }
            //    }
            //});

            //stopwatch.Stop();
            //Console.WriteLine("Dapper总计耗时：" + stopwatch.ElapsedMilliseconds + "毫秒");


            Parallel.For(0, 5, k =>
            {
                Thread.Sleep((k%2) *1000);
                Console.WriteLine("hello"+k);
            });
            Console.WriteLine("over");
            Console.ReadKey();
        }
    }

    public class BloggingContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql(@"server=127.0.0.1;database=db_test;uid=root;pwd=123456");
        }

        public DbSet<Blog> Blogs { get; set; }
    }

    [Table("tb_blog")]
    public class Blog
    {
        [Column("blog_id")]
        [Key]
        public int BlogId { get; set; }

        [Column("url")]
        public string Url { get; set; }
    }
}
